<template>
  <div class="header">
    <div class="logo"></div>
    <div class="links">
      <router-link :to="url">{{$t('登录')}}</router-link>
      <router-link to="/market">{{$t('市场')}}</router-link>
      <router-link to="/help">{{$t('帮助')}}</router-link>
      <div @click="setLang(lang)" class="lang" :class="{'cn': lang == 'en', 'en': lang == 'zh-CN'}"></div>
    </div>
  </div>
</template>

<i18n>
登录:
  en: Login
帮助:
  en: FAQ
市场:
  en: Market
</i18n>

<script>
  export default {
    data() {
      return {
        url: '/login',
        lang: ''
      }
    },
    mounted() {
      this.$nextTick(function() {
        this.lang = localStorage["lang"] || 'zh-CN'
        this.isLogin()
      });
    },
    methods: {
      isLogin() {
        var token = localStorage["token_2"];
        if (token) {
          this.url = '/home'
        } else {
          this.url = '/login'
        }
      },
      setLang(lang) {
        if (lang == 'zh-CN') {
          this.$i18n.set('en')
          this.lang = localStorage["lang"] = 'en'
        } else {
          this.$i18n.set('zh-CN')
          this.lang = localStorage["lang"] = 'zh-CN'
        }
      },
    }
  }
</script>

<style lang="scss" scoped>
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    .logo {
      width: 74px;
      height: 22px;
      background: url(/static/header.png) no-repeat;
      background-size: 100%;
    }
    .links {
      display: flex;
      a {
        color: #fff;
        margin-right: 10px;
        font-size: 16px;
      }
    }
  }
</style>

<style lang="scss">
  .lang {
    width: 24px;
    height: 24px;
  }
  .en {
    background: url(/static/en.svg) no-repeat;
    background-size: contain;
  }
  .cn {
    background: url(/static/cn.svg) no-repeat;
    background-size: contain;
  }
</style>

